<template>
    <div
        class="contract-detial-page contract-cancellation-detial"

    >
        <Card>
            <Form
                ref="contractForm"
                :model="contractForm"
                :rules="contractFormValidate"
                :label-width="100"
            >
                <Affix :offset-top="120">
                    <div style="text-align: left;"
                         v-if="isApprove">
                        <Button type="primary" @click="back()">返回</Button>
                        <template v-if="!this.params.readOnly">
                            <Button type="primary" @click="through">通过</Button>
                            <Button type="primary" @click="refuse">拒绝</Button>
                        </template>
                    </div>
                    <div style="text-align: left;" v-else>
                        <Button type="primary" @click="back()">返回</Button>
                        <template v-if="!this.params.readOnly">
                            <Button type="primary" @click="save">保存</Button>
                            <Button type="primary" @click="submit">提交</Button>
                        </template>
                    </div>

                </Affix>
                <Collapse v-model="openPanel">

                    <Panel name="0"
                           v-if="this.params.processStatus=== undefined && this.params.processStatus === 'repairedUnderReview' || this.params.processStatus === 'underReview'">
                        <!-- <Alert>日志信息</Alert> -->
                        日志信息
                        <div class="content" slot="content"
                             v-if="isApprove || this.params.processStatus === 'approved'">
                            <approveHistory :Obj="Obj"/>
                        </div>
                    </Panel>
                    <Panel name="1">
                        <!-- <Alert>基本信息</Alert> -->
                        基本信息
                        <div class="content" slot="content">
                            <BaseInfo ref="baseInfo" :formData="this.contractForm"/>
                        </div>
                    </Panel>
                    <Panel name="2">
                        <!-- <Alert>合同信息</Alert> -->
                        合同信息
                        <div class="content" slot="content">
                            <contractInfo
                                :Obj="Obj"
                                ref="contractInfo"
                            />
                        </div>
                    </Panel>
                    <Panel name="4">
                        <!-- <Alert>金融产品</Alert> -->
                        金融产品
                        <div class="content" slot="content">
                            <FinanceInfo :Obj="Obj"/>
                        </div>
                    </Panel>

                    <Panel name="6">
                        <!-- <Alert>资产信息</Alert> -->
                        资产信息
                        <div class="content" slot="content">
                            <CustAssetInfo :Obj="Obj"/>
                        </div>
                    </Panel>

                    <Panel name="5">
                        <!-- <Alert>业务办理</Alert> -->
                        业务办理
                        <div class="content" slot="content">
                            <business ref="business" :Obj="this.Obj" :params="params"
                                      @handleSubmit="handleSubmit"
                                      @findByContractNo="findByContractNo"
                                      :isApprove="isApprove"

                            />
                        </div>
                    </Panel>
                    <Panel name="10"
                           v-if="isApprove&& !this.params.readOnly">
                        <!-- <Alert>当前用户: {{userInfo ? userInfo.userRealName :''}}</Alert> -->
                        当前用户: {{userInfo ? userInfo.userRealName :''}}
                        <div class="content" slot="content">
                            <ContractCurrentUser ref="contractCurrentUser"
                                                 :Obj="Obj"/>
                        </div>
                    </Panel>
                </Collapse>
            </Form>
        </Card>
        <Spin size="large" fix v-if="spinShow"></Spin>
    </div>
</template>
<script>
    import {mapGetters} from "vuex";
    import {getDictDataList} from "_p/basic/assets/contract/js/utils";
    // 基本信息
    import BaseInfo from "_p/basic/pages/contract/common/base-info";
    // 日志信息
    import approveHistory from "_p/basic/pages/contract/common/approve-history/approve-history";
    // 金融产品
    import FinanceInfo from "_p/basic/pages/contract/common/finance-info/finance-info";
    import {channelType, loanSupervisor} from "_p/basic/api/contract/channel-type/channel-type";
    // 合同信息
    import contractInfo from "_p/basic/pages/contract/common/contract-info/contract-info";
    //业务办理
    import business from "./business";
    // 当前用户
    import ContractCurrentUser from "_p/basic/pages/contract/common/current-user/contractCurrentUser";
    // 资产信息
    import CustAssetInfo from "_p/basic/pages/contract/common/cust-asset/custAssetInfo";
    //保存豁免单信息,
    import {
        submit,
        saveProposalApplicationInfo,
        submitCostExemptBill
    } from "@/projects/basic/api/contract/cost-exempt-bill/index";
    import {findByContractNo} from "_p/basic/api/contract/activated-case/index";
    //附件校验
    import {
        checkFile,
    } from "_p/basic/api/image/image-upload";

    export default {
        // 费用豁免单详情
        name: "CostExemptBillDetail",
        components: {
            BaseInfo,
            contractInfo,
            FinanceInfo,
            CustAssetInfo,
            approveHistory,
            business,
            ContractCurrentUser,
        },
        props: {},
        data() {
            return {
                spinShow: false,
                type: null,
                isLoan: null,
                uploadFile: {
                    busiType: "",
                    busiNo: "",
                    busiData: {}
                },

                pageInfo: {},
                contractBusiness: [
                    "costExemptBill",
                    "closeContract",
                    "costExempt",
                    "collectionRevoke",
                    "exemptTypes",
                ],
                params: {},
                pageParams: {},
                contractNo: null,
                Obj: {},
                caseNo: "",
                id: null,
                loading: true,
                defaultContractData: {},
                contractForm: {},
                contractFormValidate: {},
                // 数据字典
                dicttArr: ["AccountRegistStatus", "certType", "PersonRoleType", "payBank"],
                isApprove: null,
                // selct 数据字典
                dataDict: {},
                // 弹窗状态
                // differenceModal: false,
                // overpaymentModal: false,
                modalType: "",
                modalTitle: "",
                modal: false,
                openPanel: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
            };
        },
        computed: {
            ...mapGetters({userInfo: "userInfo", roleType: "roles"}),
            // 是否是合作商
            isSP: function () {
                return channelType(this.roleType)
                // return true;
            },

        },
        created() {
            this.getParams();
        },
        mounted() {
            console.group("初始数据：");
            console.log("userInfo:", this.userInfo);
            console.log("pageParams:", this.pageParams);
            console.log("roleType:", this.roleType);
            console.log("isSP:", this.isSP);
            console.log("params:", this.params);
            console.log("isApprove:", this.isApprove);
            console.log("contractNo:", this.contractNo);
            console.log("caseNo:", this.caseNo);
            console.log("readOnly:", this.params.readOnly);
            console.log("isLoan:", this.isLoan);
            console.groupEnd();
            this.init();
            this.getDictDataList();

        },
        methods: {
            init() {
                this.getParams();
                console.log("isLoan:", this.isLoan);
                console.log("this.getParams();", this.contractNo);
            },
            // 获取初始数据
            getParams() {
                // this.$refs[this.params.readOnly].resetFields();
                const params = this.afs.getPageParams(this);
                const {processStatus} = params;
                this.isLoan = loanSupervisor(this.roleType);
                this.params = params;
                console.log("11111", params)
                this.type = params.type;
                // this.readOnly = params.type === "read" ? true : false;
                console.log(" this.readOnly ", this.params.readOnly)
                this.contractNo = params.contractNo;
                this.contractForm = params
                this.caseNo = params.caseNo;
                this.id = params.id;
                this.Obj = {
                    caseNo: params.caseNo,
                    contractNo: params.contractNo,
                    id: params.id,
                    roleType: this.isSP,
                    type: 'costExemptBill'

                }

                console.log("this.Obj", this.Obj)
                this.isApprove =
                    !!processStatus &&
                    (processStatus === "repairedUnderReview" ||
                        processStatus === "underReview" ||
                        processStatus === "submission");
                console.log("params.processStatus=" + params.processStatus);

            },
            checkFile() {
                let params;
                console.log("params", params)
                params = {
                    busiNo: this.contractForm.caseNo,
                    busiType: "costExemptBill",
                };
                console.log("params", params)
                checkFile(params).then(res => {
                    if (res.code === "0000") {
                        this.$Modal.confirm({
                            title: "确认提交",
                            content: "您确认要提交 ?",
                            onOk: () => {
                                this.$refs.business.formValidate("findByContractNo");
                            }
                        })
                        // this.findByContractNo();
                    }
                })
            },
            formData() {
                this.contractForm.contractNo = this.$refs.contractInfo.formData.contractNo;
                this.contractForm.applyNo = this.$refs.contractInfo.formData.applyNo;
                this.contractForm.clientName = this.$refs.contractInfo.formData.basicCustBaseInfo.custName;
                this.contractForm.contractStatus = this.$refs.contractInfo.formData.basicMainInfo.contractStatus;
                this.contractForm.channelFullName = this.$refs.contractInfo.formData.basicChannelInfo.channelFullName;
                this.contractForm.channelType = this.$refs.contractInfo.formData.basicChannelInfo.channelType;
                this.contractForm.startDate = this.$refs.contractInfo.formData.basicMainInfo.startDate;
                this.contractForm.certNo = this.$refs.contractInfo.formData.basicCustBaseInfo.certNo;
                this.contractForm.exemptReason = this.$refs.business.formData.exemptReason;
                this.contractForm.costAmt = this.$refs.business.formData2.costAmt;
                this.contractForm.expireCostAmt = this.$refs.business.formData2.expireCostAmt;
                this.contractForm.overdueAmt = this.$refs.business.formValidate2.costAmt;
                this.contractForm.netOverdueAmt = this.$refs.business.formValidate2.netOverdueAmt;
                this.contractForm.maxAmt = this.$refs.business.formData.maxAmt;
                this.contractForm.minAmt = this.$refs.business.formData.minAmt;
                this.contractForm.minRatio = this.$refs.business.formData.minRatio;
                this.contractForm.maxRatio = this.$refs.business.formData.maxRatio;
                this.contractForm.id = this.contractForm.id;
                this.contractForm.processPerson = this.isLoan;
                this.contractForm.caseNo = this.contractForm.caseNo;
                if (this.$refs.contractCurrentUser !== undefined) {
                    this.contractForm.reasonType = this.$refs.contractCurrentUser.formData.reasonType;
                    this.contractForm.approvalOpinion = this.$refs.contractCurrentUser.formData.approvalOpinion;
                }
                if (this.caseNo !== undefined) {
                    this.contractForm.caseNo = this.caseNo;
                }
            },
            // 审批 请求
            // Business 组件数据
            childBusinessForm(data) {
                if (!data) return;
                let bussiness = this.formatData(data);
                // 验证 contractCurrentUser 组件数据 传给下个方法
                this.$refs.contractCurrentUser.formValidate("childUserForm", bussiness);
            },
            save() {
                if (this.$refs.business) {
                    this.showSpin();
                    console.log("this.contractForm1000000", this.contractForm)
                    this.contractForm.maxAmt = this.$refs.business.formData.maxAmt
                    this.contractForm.minAmt = this.$refs.business.formData.minAmt
                    if (this.contractForm.maxAmt < this.contractForm.minAmt) {
                        this.$Message.error("豁免最小值必须小于最大值!");
                        return;
                    }
                    this.$refs.business.formValidate("handleSubmit");
                }
            },
            handleSubmit() {
                this.contractForm.maxAmt = this.$refs.business.formData.maxAmt
                this.contractForm.minAmt = this.$refs.business.formData.minAmt
                this.contractForm.costAmt = this.contractForm.costAmt
                // if (this.contractForm.maxAmt < this.contractForm.minAmt) {
                //     this.showSpin();
                //     this.$Message.error("豁免最小值必须小于最大值!");
                //     return;
                // }
                // if (this.contractForm.maxAmt > this.contractForm.costAmt) {
                //     this.showSpin();
                //     this.$Message.error("豁免金额需要小于费用金额!");
                //     return;
                // }
                this.formData();
                console.log("this.contractForm", this.contractForm);
                submit(this.contractForm)
                    .then(res => {
                        if (res.code === "0000") {
                            this.pageInfo = res.data;
                            this.$Message.success("保存成功!");
                            // this.$refs.business.formData = this.params;
                            console.log("res.data", res.data);
                            this.contractForm.id = res.data.id;
                            this.contractForm = res.data;
                            console.log("this.contractForm", this.contractForm);
                            this.contractForm.caseNo = res.data.caseNo;
                            this.params.caseNo = res.data.caseNo;
                            console.log("this.params", this.params)
                        }
                    })
                // .catch(e => {
                //     this.$Message.error("保存失败,请填写正确信息!");
                //     console.log(e);
                // });

            },
            submit() {
                console.log("this.contractForm", this.contractForm);
                console.log("this.$refs.business", this.$refs.business);
                this.checkFile();
                // this.$Modal.confirm({
                //     title: "确认提交",
                //     content: "您确认要提交 ?",
                //     onOk: () => {
                //         this.$refs.business.formValidate("findByContractNo");
                //     }
                // })

            },
            findByContractNo() {
                let params = {
                    contractBusiness: this.contractBusiness,
                    contractNo: this.contractNo,
                };
                console.log("params.contractBusiness", this.contractBusiness),
                    findByContractNo(params).then(res => {
                        if (res.code === "0000") {
                            if (res.msg === "success") {
                                this.fnSubmit();
                            }
                        }
                    });
            },
            fnSubmit() {
                this.formData();
                this.uploadFile.busiNo = this.contractForm.caseNo;
                this.uploadFile.busiType = 'costExemptBill';
                this.contractForm.maxAmt = this.$refs.business.formData.maxAmt
                this.contractForm.minAmt = this.$refs.business.formData.minAmt
                if (this.contractForm.maxAmt < this.contractForm.minAmt) {
                    this.$Message.error("豁免最小值必须小于最大值!");
                    return;
                }
                // //  影像附件校验
                // if (!checkFile(this.uploadFile))
                //     if (!this.params) return;
                this.contractForm.approvalOperating = 'suggestCheckF';
                submitCostExemptBill(this.contractForm).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.msg);
                        this.back();
                    }
                });
            },
            through() {
                this.$Modal.confirm({
                    title: "确认通过",
                    content: "您确认要通过 ?",
                    onOk: () => {
                        this.formData();
                        this.contractForm.id = this.params.id;
                        console.log("this.contractForm", this.contractForm)
                        if (this.readOnly) return;
                        this.contractForm.approvalOperating = 'suggestCheckF';
                        saveProposalApplicationInfo(this.contractForm).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success(res.msg);
                                this.back();
                            }
                        });
                    }
                })
            },
            goBack() {
                this.$Modal.confirm({
                    title: "确认退回",
                    content: "您确认要退回 ?",
                    onOk: () => {
                        this.formData();
                        if (this.readOnly) return;
                        this.contractForm.id = this.params.id;
                        this.contractForm.approvalOperating = 'sendBack';
                        saveProposalApplicationInfo(this.contractForm).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success(res.msg);
                                this.back();
                            }
                        });
                    }
                })
            },
            refuse() {
                this.$Modal.confirm({
                    title: "确认拒绝",
                    content: "您确认要拒绝 ?",
                    onOk: () => {
                        this.formData();
                        if (this.readOnly) return;
                        this.contractForm.id = this.params.id;
                        this.contractForm.approvalOperating = 'suggestRejectF';
                        saveProposalApplicationInfo(this.contractForm).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success(res.msg);
                                this.back();
                            }
                        });
                    }
                })
            },
            back() {
                //关闭页面
                this.afs.closeTab(this);
            },
            getDictDataList() {
                getDictDataList(this.dicttArr).then(res => {
                    console.log("getDictDataList(this.dicttArr)", res);
                    this.dataDict = res;
                });
            },
            showSpin() {
                this.$emit("showSpin");
            },

        },
    };
</script>
<style lang="less">
    @import url("../../../../styles/common/form.less");

    .contract-cancellation-detial-btns {
        padding-bottom: 16px;
    }

    .contract-cancellation-detial {
        .ivu-collapse-header {
            background-color: #f0faff;
        }
    }
</style>
